package view.components.tab
{
	import events.TabEvent;
	import utils.ColorTool;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class Tab extends Sprite
	{
		private var tabs:Array;
		private var currentTabBar:ITabBar;
		private var padding:int;
		
		public function Tab(padding:int=0)
		{
			this.padding = padding;
			init();
			
		}
		
		private function init():void
		{
			tabs = [];
		}
		
		public function addTabBar(tabBar:ITabBar,isOn:Boolean = false):void
		{
			tabs.push(tabBar);
			tabBar.self.x = (tabBar.self.width + padding )* (tabs.length - 1) ;
			tabBar.index = tabs.length - 1;
			tabBar.switchOnOff(isOn);
			tabBar.onFunction = onFunction;
			addChild(tabBar.self);
			if (isOn)
			{
				currentTabBar = tabBar;
			}
		}
		
		public function get selectedIndex():int
		{
			if (currentTabBar)
			{
				return currentTabBar.index;
			}
			return - 1;
		}
		
		public function set selectedIndex(v:int):void
		{
			if (v < 0 || v >= tabs.length)
			{
				return;
			}
			
			if (currentTabBar)
			{
				currentTabBar.switchOnOff(false);
			}
			
			tabs[v].switchOnOff(true);
			currentTabBar = tabs[v];
			var e:TabEvent = new TabEvent(TabEvent.TABBAR_CLICK);
			e.index = v;
			dispatchEvent(e);
		}
		
		private function onFunction(event:MouseEvent):void
		{
			if (currentTabBar)
			{
				currentTabBar.switchOnOff(false);
			}
			
			var tabBar:ITabBar = event.target as ITabBar;
			tabBar.switchOnOff(true);
			currentTabBar = tabBar;
			var e:TabEvent = new TabEvent(TabEvent.TABBAR_CLICK);
			e.index = tabBar.index;
			dispatchEvent(e);
		}
		
		override public function set mouseEnabled(enabled:Boolean) : void
		{
			super.mouseEnabled;
			if (enabled == false)
			{
				ColorTool.desaturation(this);
			}
			else
			{
				ColorTool.saturation(this);
			}
		}
	}
}